// 数据表格中重用的代码
import { SearchOutlined } from "@ant-design/icons-vue";

export const DataTableMixin = {
    components: {
        SearchOutlined,
    },
    data() {
        return {
            // 请求参数
            postParams: {},
            // 选中的行ID
            selectedRowKeys: [],
            // 选中的行数据
            selectedRows: [],
            // 表格数据
            dataSource: [],
            // 分页信息
            pageInfo: {
                currentPage: 1,
                pageSize: 20,
            },
            total: 0,
            loading: false,
            windowHeight: document.documentElement.clientHeight,
            pagehref: "",
            // 字段搜索关键字
            searchKeyword: ""
        }
    },
    computed: {
        // 动态获取scrollX,防止数组固定列的大小变化
        scrollX() {
            return this.columns.reduce((preVal, curVal) => {
                return preVal + curVal.width
            }, 0)
        }
    },
    props: {
        params: Object,
    },
    methods: {
        // 重置页码及选中
        reset() {
            // 恢复页码
            this.pageInfo.currentPage = this.$options.data().pageInfo.currentPage;
            // 清空选中的选项
            this.selectedRowKeys = [];
            this.selectedRows = [];
        },
        // 选择行
        onSelectChange(selectedRowKeys, selectedRows) {
            this.selectedRowKeys = selectedRowKeys;
            this.selectedRows = selectedRows;
        },
        // 分页/排序操作
        onChange(pagination, filter, sorter) {
            console.log(pagination)
            // 分页
            this.pageInfo.currentPage = pagination.current;
            this.pageInfo.pageSize = pagination.pageSize;
            // 排序
            if (sorter?.order) {
                this.pageInfo.sortIndex = sorter.field;
                this.pageInfo.sortOrder = sorter.order == "ascend" ? "asc" : "desc";
            } else {
                this.pageInfo.sortIndex = undefined;
                this.pageInfo.sortOrder = undefined;
            }
            // 筛选
            // if (Object.keys(filter).length > 0) {
            //     this.pageInfo.filter = filter;
            // }

            if (this.filterType && this.filterType !== '') {
                // 8大库，数据筛选操作时的分页
                this.projectFilter(this.filterType, null);
            } else {
                // 正常分页
                this.getList();
            }
        },
        // 字段搜索操作
        handleSearch(confirm) {
            confirm();
        },
        // 搜索重置
        handleSearchReset(clearFilters) {
            clearFilters({ confirm: true })
        },
        // 改变列宽
        handleResizeColumn(w, col) {
            col.width = w;
        },
        //筛选表的列
        columnsCheck(checked, data, index) {
            data[index].show = checked;
        },
    },
    created() {
        this.pagehref = location.hash;
        // 请求数据
        this.getList();
    },
}